package ejemplosAR;

import processing.core.*;
import processing.video.*;
import jp.nyatla.nyar4psg.*;

public class MultiMarkerTest extends PApplet {

	private static final long serialVersionUID = 1L;
	private static final String PARA = "/Users/marthagarzon/Desktop/Juan Manuel/Workspace/ModuloSimulacion/data/camera_para.dat";
	private static final String HIRO = "/Users/marthagarzon/Desktop/Juan Manuel/Workspace/ModuloSimulacion/data/patt.hiro";
	private static final String KANJI ="/Users/marthagarzon/Desktop/Juan Manuel/Workspace/ModuloSimulacion/data/patt.kanji";

	private PFont font=createFont("FFScala", 32);
	private Capture cam;
	private MultiMarker nya;
	private int c=0;

	@SuppressWarnings("static-access")
	public void setup() {
		size(640,480,P3D);
		colorMode(RGB, 100);
		cam=new Capture(this,width,height);
		nya=new MultiMarker(this,width,height,PARA,new NyAR4PsgConfig(NyAR4PsgConfig.CS_RIGHT_HAND,NyAR4PsgConfig.TM_NYARTK));
		nya.setARClipping(100,1000);
		nya.setConfidenceThreshold(0.6);
		nya.addARMarker(HIRO,80);
		nya.addARMarker(KANJI,80);
		nya.addNyIdMarker(31,80);
		println(nya.VERSION); 
	}
	public void draw() {
		c++;
		if (cam.available()) {
			background(255);
			cam.read();
			nya.drawBackground(cam);
			nya.detect(cam);
			if(nya.isExistMarker(0)){
				nya.beginTransform(0);
				{
					drawBox(255,0,0);
					drawMarkerXYPos(0);
				}
				nya.endTransform();  
				drawMarkerPatt(0);
				drawVertex(0);
				PVector p=nya.marker2ScreenCoordSystem(0,0,0,0);
				noFill();
				ellipse(p.x,p.y,200,200);
			}
			if(nya.isExistMarker(1)){
				nya.beginTransform(1);
				{
					drawBox(0,255,0);
					drawMarkerXYPos(1);
				}
				nya.endTransform();  
				drawMarkerPatt(1);
				drawVertex(1);
			}
			if(nya.isExistMarker(2)){
				nya.beginTransform(2);
				{
					drawBox(0,0,255);
					drawMarkerXYPos(2);
					println(nya.getLife(2));
				}
				nya.endTransform(); 
				drawMarkerPatt(2);
				drawVertex(2);
			}
		}
	}

	public void drawBox(int ir,int ig,int ib)
	{
		pushMatrix();
		drawgrid();
		fill(ir,ig,ib);
		stroke(255,200,0);
		translate(0,0,20);
		box(40);
		noFill();
		translate(0,0,-20);
		rect(-40,-40,80,80); 
		popMatrix();
	}

	public void drawMarkerPatt(int id)
	{
		PImage p=nya.pickupMarkerImage(id,
				40,40,
				-40,40,
				-40,-40,
				40,-40,
				100,100);
		//  PImage p=nya.pickupRectMarkerImage(id,-40,-40,80,80,100,100);
		image(p,id*100,0);
	}

	public void drawMarkerXYPos(int id)
	{
		pushMatrix();
		PVector pos=nya.screen2MarkerCoordSystem(id,mouseX,mouseY);
		translate(pos.x,pos.y,0);
		noFill();
		stroke(0,0,100);
		ellipse(0,0,20-c%20,20-c%20);
		popMatrix();
	}

	public void drawVertex(int id)
	{
		PVector[] i_v=nya.getMarkerVertex2D(id);
		textFont(font,(float) 10.0);
		stroke(100,0,0);
		for(int i=0;i<4;i++){
			fill(100,0,0);
			ellipse(i_v[i].x,i_v[i].y,6,6);
			fill(0,0,0);
			text("("+i_v[i].x+","+i_v[i].y+")",i_v[i].x,i_v[i].y);
		}
	}

	public void drawgrid()
	{
		pushMatrix();
		stroke(0);
		strokeWeight(2);
		line(0,0,0,100,0,0);
		textFont(font,(float) 20.0); text("X",100,0,0);
		line(0,0,0,0,100,0);
		textFont(font,(float) 20.0); text("Y",0,100,0);
		line(0,0,0,0,0,100);
		textFont(font,(float) 20.0); text("Z",0,0,100);
		popMatrix();
	}
}
